<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'controllers/admin.php';

/**
 * Контроллер просмотра и редактирования основного меню сайта
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class Admin_Site_Menu extends Admin {
   
   /**
    * Конструктор класса
    *
    * @access public
    * @return Admin_Site_Menu
    */
   function Admin_Site_Menu() {
      parent::Admin();
      $this->set_menu(4, 1);
      $this->load->language('button');
   } // end Admin_Site_Menu
   
   /**
    * Метод по умолчанию
    *
    * @access public
    */
   function index() {
      $data = array();
      $this->load->helper('link');
      $this->load->model('m_select_site_menu');
      $this->load->library('table_builder');
      $this->table_builder->set_model($this->m_select_site_menu);
      $this->table_builder->add_button(create_link($this->get_controller(), 'activate'), $this->lang->line('activate_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'pause'), $this->lang->line('pause_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'update_positions'), $this->lang->line('update_positions_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'add'), $this->lang->line('add_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'edit'), $this->lang->line('edit_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'delete'), $this->lang->line('delete_button'), $this->lang->line('site_menu_delete_confirmation'));
      $data['table'] = $this->table_builder->generate();
      $this->set_content($this->parser->parse('admin/site_menu.html', $data, true));
      $this->display();
   } // end index
   
   /**
    * Метод активации элементов меню
    *
    * @access public
    */
   function activate() {
      $ids = $this->input->post('ids');
      if (is_array($ids) && 0 < count($ids)) {
         $this->load->model('m_site_menu');
         foreach ($ids as $id) {
            $this->m_site_menu->set_id($id);
            $this->m_site_menu->activate();
         }
         $this->set_message($this->lang->line('activate_menu_items_succ'), 'info', true);
      }
      $this->redirect($this->get_controller());
   } // end activate
   
   /**
    * Метод приостановки элементов меню
    *
    * @access public
    */
   function pause() {
      $ids = $this->input->post('ids');
      if (is_array($ids) && 0 < count($ids)) {
         $this->load->model('m_site_menu');
         foreach ($ids as $id) {
            $this->m_site_menu->set_id($id);
            $this->m_site_menu->pause();
         }
         $this->set_message($this->lang->line('pause_menu_items_succ'), 'info', true);
      }
      $this->redirect($this->get_controller());
   } // end pause
   
   /**
    * Метод удаления элементов меню
    *
    * @access public
    */
   function delete() {
      $ids = $this->input->post('ids');
      if (is_array($ids) && 0 < count($ids)) {
         $this->load->model('m_site_menu');
         foreach ($ids as $id) {
            $this->m_site_menu->set_id($id);
            $this->m_site_menu->delete();
         }
         $this->set_message($this->lang->line('delete_menu_items_succ'), 'info', true);
      }
      $this->redirect($this->get_controller());
   } // end delete
   
   /**
    * Метод добавления элементов меню
    * Редиректит на нужный контроллер
    *
    * @access public
    */
   function add() {
      $this->redirect('admin_update_site_menu');
   } // end add
   
   /**
    * Метод изменения элементов меню
    * Редиректит на нужный контроллер
    *
    * @access public
    */
   function edit() {
      $ids = $this->input->post('ids');
      if (is_array($ids) && 0 < count($ids)) {
         $id = array_shift($ids);
         $this->redirect('admin_update_site_menu', '', array('id' => $id));
      }
      $this->redirect($this->get_controller());
   } // end edit
   
   /**
    * Метод установки позиций элементам меню
    *
    * @access public
    */
   function update_positions() {
      $positions = $this->input->post('positions');
      if (is_array($positions) && 0 < count($positions)) {
         $this->load->model('m_site_menu');
         foreach ($positions as $id => $position) {
            $this->m_site_menu->set_id($id);
            $this->m_site_menu->set('position', $position);
            $this->m_site_menu->update();
         }
         $this->set_message($this->lang->line('update_positions_menu_items_succ'), 'info', true);
      }
      $this->redirect($this->get_controller());
   } // end update_positions
   
}
